package SCAnalyzer;


import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import smc.Concept;
import smc.Document;
import smc.RelatedConcept;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author COMPAQ
 */
public class MapAdjust extends javax.swing.JFrame {

    /**
     * Creates new form MapAdjust
     */
    //tt
    List paths;
    String absolutePath;
    Document newDoc;
    ArrayList<Document> documents;
    String message;
    public MapAdjust() {
        initComponents();
         paths=new ArrayList();
         documents=new ArrayList<Document>();
         jButton7.setEnabled(false);
         jButton6.setEnabled(false);
         jSlider1.setEnabled(false);
         jButton4.setEnabled(false);
         jButton2.setEnabled(false);
         jButton3.setEnabled(false);
         jButton1.setEnabled(false);
         try {
                    String temp=new File("oo").getCanonicalPath();
                    int a=temp.lastIndexOf("\\");
                    absolutePath=temp.substring(0,a);
                    
                } catch (IOException ex) {
                    Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
                }
         
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        panel1 = new java.awt.Panel();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Adjust Frame");

        jLabel1.setText("File Location");

        jLabel2.setText("Map View");

        jButton4.setText("Delete Relationships");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(jList1);

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 789, Short.MAX_VALUE)
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 565, Short.MAX_VALUE)
        );

        jButton5.setText("Close");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Save Changes");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Upload the Document");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Browse Map");

        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jButton1.setText("Adjust Map");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Add concept");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Add Relationships");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(jButton1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap(947, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 33, Short.MAX_VALUE))
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLabel1)
                        .addGap(27, 27, 27)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addComponent(jButton4)
                                .addGap(18, 18, 18)
                                .addComponent(jButton6)
                                .addGap(18, 18, 18)
                                .addComponent(jButton7)
                                .addGap(26, 26, 26)
                                .addComponent(jButton5)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton8)
                                .addGap(13, 13, 13)))))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        
        System.exit(0);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged

       
       
       
        
    }//GEN-LAST:event_jList1ValueChanged

    public void mapReload(Document doc){
    String actualpath=(String) paths.get(jList1.getSelectedIndex());
      // Document doc = documents.get(jList1.getSelectedIndex());
    System.out.println("Size of the document for the map reloading");
        System.out.println(doc.getDoc().size());
        System.out.println("now creating the js creator");
        doc.printDoc();
       JSCreator jsc = new JSCreator();
       //testing document
       doc.testDocForRelations();
       //done testing

        try {
           jsc.CreateJS(doc,actualpath);


        } catch (IOException ex) {
            Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        NativeInterface.open();
        panel1.removeAll();
          SwingUtilities.invokeLater(new Runnable() {
      public void run() {
                

          InnerWeb a=new InnerWeb(absolutePath+"\\visual\\example2.html");
          JScrollPane scrollPane = new JScrollPane(a);
          a.setBounds(0, 0, 700, 530);
          a.setVisible(true);
          
        panel1.add(a);
          
       
      }
    });
    }
    
    
    
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        try {
             //TODO add your handling code here:
           jButton7.setEnabled(true);
           jButton6.setEnabled(true);
           jSlider1.setEnabled(true);
           jButton4.setEnabled(true);
           jButton2.setEnabled(true);
           jButton1.setEnabled(true);
           jButton3.setEnabled(true);
           FileWriter f1 = new FileWriter(absolutePath+"\\visual\\rnodes.dpi");
           f1.write("");
           f1.close();
         } catch (IOException ex) {
            Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(jList1.getSelectedIndex()); 
        System.out.println("You have selected "+paths.get(jList1.getSelectedIndex()));
    //   System.out.println("You have selected file URI"+documents.get(jList1.getSelectedIndex()).getUri());
        String actualpath=(String) paths.get(jList1.getSelectedIndex());
        Document doc = documents.get(jList1.getSelectedIndex());
        DocumentFilter docFilter=new DocumentFilter();
        newDoc=docFilter.filterDocument(doc, 50);
        jSlider1.setValue(50);
        JSCreator jsc = new JSCreator();

        try {
           jsc.CreateJS(newDoc,actualpath);


        } catch (IOException ex) {
            Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        NativeInterface.open();
        panel1.removeAll();
          SwingUtilities.invokeLater(new Runnable() {
      public void run() {
                

          InnerWeb a=new InnerWeb(absolutePath+"\\visual\\example2.html");
          JScrollPane scrollPane = new JScrollPane(a);
          a.setBounds(0, 0, 700, 530);
          a.setVisible(true);
          
        panel1.add(a);
          
       
      }
    });
    }//GEN-LAST:event_jList1MouseClicked

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        FileReader fr = null;
        FileWriter fw=null;
        ArrayList<String> deletedConcepts=new ArrayList<String>();
        try {
            fr = new FileReader(absolutePath+"\\visual\\rnodes.dpi"); //read deleted concepts from file
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
        }

        
        BufferedReader in = new BufferedReader(fr);
        String line;
        try {
            while ((line = in.readLine()) != null)
              deletedConcepts.add(line);      //add delted concepts to list
        } catch (IOException ex) {
            Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        if(deletedConcepts.isEmpty()){
            JOptionPane.showMessageDialog(null, "No changes are done");
        }
        else{
        System.out.println("Deleted concpets ="+deletedConcepts);
        Document doc = documents.get(jList1.getSelectedIndex());

        String message="You are going to delete '";
        int j=0;

        for(String con:deletedConcepts){

             j++;
             if(j==1){
                 message=message+con+" ";
             }
             else if(j==deletedConcepts.size()){
                  message=message+",'"+con+"'.";
             }
             else{
                message=message+",'"+con;
             }
        }
      
        int answer=JOptionPane.showConfirmDialog(null, message);
        if(answer==0){
            for(String con:deletedConcepts){
                doc.deleteConcept(con);
                newDoc.deleteConcept(con);
            }
            System.out.println("deleted");

          
            }
        mapReload(newDoc);
        System.out.println("deleted");
        //testing delteded doc concept
        System.out.println("Testing doc after deleteing concept :::::::::");
        newDoc.testDocForRelations();
        System.out.println("doc testing done successfully after deleting concepts");

        mapReload(newDoc);

        
         try {
            fw = new FileWriter(absolutePath+"\\visual\\rnodes.dpi");
            
         } catch (IOException ex) {
            Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
         }
           BufferedWriter out = new BufferedWriter(fw);
        
           try {
                out.write("");
            } 
           catch (IOException ex) {
                Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
            }
           finally{
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(MapAdjust.class.getName()).log(Level.SEVERE, null, ex);
            }
           }
        } 
     
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        
       // Document doc = documents.get(jList1.getSelectedIndex());
        DocumentUploader docloader=new DocumentUploader(newDoc);
        docloader.start();
        System.out.println(newDoc.getName()+ " is selected" ); 
       
    }//GEN-LAST:event_jButton7ActionPerformed

    int sliderValue;
    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
        // TODO add your handling code here:
       // System.out.println(jSlider1.getValue());
        sliderValue=jSlider1.getValue();
       
        
    }//GEN-LAST:event_jSlider1StateChanged

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        DeleteRelation deleteRel=new DeleteRelation();
        deleteRel.setVisible(true);
        deleteRel.setLocationRelativeTo(null);
        Document doc = documents.get(jList1.getSelectedIndex());
        HashMap<String, Concept> mp = newDoc.getDoc();
      //  System.out.println(doc1.keySet());
        deleteRel.setNodeData(mp,doc,this,newDoc);
        
        
    }//GEN-LAST:event_jButton4ActionPerformed

    
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Document doc = documents.get(jList1.getSelectedIndex());
        //checking the size of the document
        System.out.println("Testing the size jbutton1 action perfomed");
        System.out.println("passed doc size to the method is :"+doc.getDoc().size());
        //endi checking size
        DocumentFilter docFilter=new DocumentFilter();
        newDoc=docFilter.filterDocument(doc, sliderValue);
        //tdesting new doc obtained to cover persentage
        System.out.println("Testing the doc obtained for showing percentage");
        newDoc.testDocForRelations();
        System.out.println(newDoc.getDoc().size());
        System.out.println("new doc to show percentage of map is done successfully");
        //end testing
        mapReload(newDoc);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
       // JOptionPane.showInputDialog(message);
        String head="Concept";
        message=JOptionPane.showInputDialog(null, "Enter the name of new concpet", head, 1);
        AddConcept add=new AddConcept();
        Document doc = documents.get(jList1.getSelectedIndex());
        if(newDoc.getDoc().keySet().contains(message)){
            JOptionPane.showMessageDialog(null, "New concept is available in the Map");
        }
        else if(message.length()<3){
             JOptionPane.showMessageDialog(null, "Invalid concept length");
        }
        else{
        add.analyzeData(newDoc,message,this,doc);
        this.setVisible(false);
        add.setVisible(true);
        add.setLocationRelativeTo(null);
       }
        
       
        
                
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        
        this.setVisible(false);
        AddRelation addRelation=new AddRelation();
        addRelation.setVisible(true);
        addRelation.setLocationRelativeTo(null);
        Document doc = documents.get(jList1.getSelectedIndex());
        HashMap<String, Concept> mp = newDoc.getDoc();
      //  System.out.println(doc1.keySet());
        addRelation.setNodeData(mp,doc,this,newDoc);
        
        
    }//GEN-LAST:event_jButton3ActionPerformed

    public void showLocation(String file){
        
        paths.add(file);
        
        
    }
    
    
    public void setDocumentsObjects(Document doc){
        documents.add(doc);
    }
    
    public ArrayList<Document> getDocumentObject(){
        return documents;
    }
    
    public void showPaths() throws IOException{
        System.out.println(paths);
       
        jList1.setListData(paths.toArray());
        
  
        
         // UIUtils.setPreferredLookAndFeel();
          NativeInterface.open();
          SwingUtilities.invokeLater(new Runnable() {
          public void run() {
      
            InnerWeb a=new InnerWeb(absolutePath+"\\visual\\html\\First.html");
            JScrollPane scrollPane = new JScrollPane(a);
            a.setBounds(0, 0, 700, 530);
            a.setVisible(true);
          
        panel1.add(a);
          
       
      }
    });
//    NativeInterface.runEventPump();
    }

       
    
    
    
    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSlider jSlider1;
    private java.awt.Panel panel1;
    // End of variables declaration//GEN-END:variables
}
